#define _CRT_SECURE_NO_WARNINGS


#include <stdio.h>

void PrintArrar(int* a, int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void Swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

void SelectSort(int* a, int n)
{
	int begin = 0;
	int end = n - 1;

	while (begin < end)
	{
		int min = begin;
		int max = begin;
		for (int i = begin; i <= end; ++i)
		{
			if (a[i] < a[min])
			{
				min = i;
			}
			if (a[i] > a[max])
			{
				max = i;
			}
		}
		Swap(&a[begin], &a[min]);
		if (begin == max)
		{
			max = min;
		}
		Swap(&a[max], &a[end]);
		++begin;
		--end;
	}
}

void TestSelectSort()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1 };
	SelectSort(arr, sizeof(arr) / sizeof(int));
	PrintArrar(arr, sizeof(arr) / sizeof(int));
}

int main()
{
	TestSelectSort();
	return 0;
}